package com.qys.livesMall.user.controller.sys;


import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.view.BaseVO;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.system.source.service.ISysMenuService;
import com.qys.livesMall.user.domain.dto.role.*;
import com.qys.livesMall.user.domain.vo.role.RoleMenuTree;
import com.qys.livesMall.user.domain.vo.role.SysRoleVO;
import com.qys.livesMall.user.service.ISysPermissionService;
import com.qys.livesMall.user.service.ISysRoleMenuService;
import com.qys.livesMall.user.service.ISysRoleService;
import com.qys.livesMall.user.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 *
 * @author wanglei
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseVO {
    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysPermissionService permissionService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysMenuService iSysMenuService;

    @Autowired
    private ISysRoleMenuService roleMenuService;


    /**
     * 角色列表
     *
     * @param search
     * @return
     */
    @GetMapping("/list")
    public R<QysPage<SysRoleVO>> list(SysRoleSearch search) {
        return R.ok(roleService.selectRoleList(search));
    }


    /**
     * 根据角色编号获取详细信息
     */
    @GetMapping(value = "/{roleId}")
    public R<SysRoleVO> getInfo(@PathVariable Long roleId) {
        return R.ok(roleService.selectRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @PostMapping
    public R add(@Validated @RequestBody AddRole role) {
        if (roleService.checkRoleNameExist(role.getRoleName())) {
            return R.fail("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        return R.ok(roleService.insertRole(role));

    }

    /**
     * 修改保存角色
     */
    @PutMapping
    public R edit(@Validated @RequestBody EditRole role) {
        if (roleService.checkRoleNameExist(role.getRoleName())) {
            return R.fail("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }

        return roleService.updateRole(role);
    }

    /**
     * 状态修改
     */
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody EditRoleStatus role) {
        return roleService.updateRoleStatus(role);
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{roleIds}")
    public R remove(@PathVariable Long[] roleIds) {
        return R.ok(roleService.deleteRoleByIds(roleIds));
    }

    /**
     * 获取角色选择框列表
     */
    @GetMapping("/optionselect")
    public R<List<SysRoleVO>> optionselect() {
        return R.ok(roleService.selectRoleAll());
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeOfSelect/{roleId}")
    @ResponseBody
    public R<RoleMenuTree> roleMenuTreeOfSelect(@PathVariable("roleId") Long roleId) {
        return R.ok(roleMenuService.roleMenuTreeOfSelect(roleId));
    }

    @PostMapping("/setRoleMenu")
    @ResponseBody
    public R setRoleMenu(@RequestBody EditRoleMenu roleMenu){
        return roleMenuService.insertRoleMenu(roleMenu);
    }


//    /**
//     * 查询未分配用户角色列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:role:list')")
//    @GetMapping("/authUser/unallocatedList")
//    public TableDataInfo unallocatedList(SysUser user)
//    {
//        startPage();
//        List<SysUser> list = userService.selectUnallocatedList(user);
//        return getDataTable(list);
//    }
//
//    /**
//     * 取消授权用户
//     */
//
//    @PutMapping("/authUser/cancel")
//    public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole)
//    {
//        return toAjax(roleService.deleteAuthUser(userRole));
//    }
//
//    /**
//     * 批量取消授权用户
//     */
//    @PreAuthorize("@ss.hasPermi('system:role:edit')")
//    @Log(title = "角色管理", businessType = BusinessType.GRANT)
//    @PutMapping("/authUser/cancelAll")
//    public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds)
//    {
//        return toAjax(roleService.deleteAuthUsers(roleId, userIds));
//    }

//    /**
//     * 批量选择用户授权
//     */
//    @PutMapping("/authUser/selectAll")
//    public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds)
//    {
//        roleService.checkRoleDataScope(roleId);
//        return toAjax(roleService.insertAuthUsers(roleId, userIds));
//    }

}
